package com.woniuxy.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.common.dto.OwnerDto;
import com.woniuxy.infrastructure.mapper.MeterReadingMapper;
import com.woniuxy.infrastructure.pojo.MeterReading;
import com.woniuxy.infrastructure.service.MeterReadingService;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.woniuxy.common.utils.UserUtil.getUser1;

/**
* @author yolo
* @description 针对表【meter_reading(物业抄表记录)】的数据库操作Service实现
* @createDate 2025-07-15 18:00:14
*/
@Service
public class MeterReadingServiceImpl extends ServiceImpl<MeterReadingMapper, MeterReading> implements MeterReadingService {

    @Override
    public List<MeterReading> query(MeterReading meterReading) {
        LambdaQueryWrapper<MeterReading> queryWrapper = new LambdaQueryWrapper<>();
        if (meterReading.getRoomId() != null) {
            queryWrapper.eq(MeterReading::getRoomId, meterReading.getRoomId());
        }
        if (meterReading.getOwnerId() != null) {
            queryWrapper.eq(MeterReading::getOwnerId, meterReading.getOwnerId());
        }
        if (meterReading.getMeterType() != null) {
            queryWrapper.eq(MeterReading::getMeterType, meterReading.getMeterType());
        }
        if (meterReading.getMeterNumber() != null) {
            queryWrapper.eq(MeterReading::getMeterNumber, meterReading.getMeterNumber());
        }
        if (meterReading.getReadDate() != null) {
            queryWrapper.eq(MeterReading::getReadDate, meterReading.getReadDate());
        }
        if (meterReading.getReader() != null) {
            queryWrapper.eq(MeterReading::getReader, meterReading.getReader());
        }
        queryWrapper.orderByDesc(MeterReading::getReadDate);
        return this.list(queryWrapper);
    }

    @Override
    public List<MeterReading> queryAll() {
        // 创建查询，按抄表日期降序排序
        LambdaQueryWrapper<MeterReading> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(MeterReading::getReadDate);
        return this.list(queryWrapper);
    }

    @Override
    public boolean addMeterReading(MeterReading meterReading) {
        return this.save(meterReading);
    }

    @Override
    public List<MeterReading> payList() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 从Authorization头中获取Token
        String token = request.getHeader("Authorization");
        OwnerDto user = getUser1(token);
        return lambdaQuery().eq(MeterReading::getOwnerId, user.getId()).eq(MeterReading::getStatus,0).list();
    }

    @Override
    public List<MeterReading> paidList() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 从Authorization头中获取Token
        String token = request.getHeader("Authorization");
        OwnerDto user = getUser1(token);
        return lambdaQuery().eq(MeterReading::getOwnerId, user.getId()).eq(MeterReading::getStatus,1).list();
    }

    @Override
    public String pay() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 从Authorization头中获取Token
        String token = request.getHeader("Authorization");
        OwnerDto user = getUser1(token);

        boolean b = lambdaUpdate().eq(MeterReading::getOwnerId, user.getId())
                .eq(MeterReading::getStatus, 0)
                .set(MeterReading::getStatus, 1)
                .update();
        return b?"支付成功":"支付失败";
    }
}




